package edu.chinasoft.servlet;

import java.io.IOException;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.annotation.WebServlet;
import org.apache.log4j.Logger;

import edu.chinasoft.pojo.PageUtilForUser;
import edu.chinasoft.pojo.User;
import edu.chinasoft.service.UserService;
import edu.chinasoft.service.UserServiceImp;

/**
 * 
 * @author cjogng
 * @date 2020年1月17日:上午11:18:09
 * @version V1.0
 * @Description: 查询用户
 */
@WebServlet(name = "myFindUserPageForServlet", value = "/servlet/finduserpageforservlet")
public class FindUserPageForServlet extends HttpServlet {
	private static Logger logger = Logger.getLogger(FindUserPageForServlet.class);
	private UserService userService = new UserServiceImp();

	/**
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		logger.debug(" FindUserPageForServlet >doGet start.......");
		response.setContentType("text/html;charset=utf-8");
		request.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();

		// 获取分页的用户
		
		//获取当前页，如果没有，则默认为1
		String currentPageString = request.getParameter("currentPage");
		int currentPage=currentPageString==null?1:Integer.parseInt(currentPageString);
		
		//读取配置文件里的信息，设置关于商品的分页单位
		ServletContext servletContext = this.getServletContext();
		String pageUtilForUserString = servletContext.getInitParameter("PageUtilForUser");
		int pageUtil = Integer.parseInt(pageUtilForUserString);
		
		

		PageUtilForUser pageUtilForUser = userService.findUser(currentPage, pageUtil);

		List<User> userList = pageUtilForUser.getUserList();
		out.write(
				"<!DOCTYPE html>     <html lang='en'>     <head>     	<meta charset='UTF-8'>     	<title>查看所有用户</title>     </head>     <body>      <h2 align='center'>所有用户</h2>      <table width='60%' align='center' border='1px' cellpadding='0' cellpadding='0'>       <tr align='center'>        <td>编号</td>        <td>用户姓名</td>        <td>备注</td>        <td>操作</td>       </tr>");

		// 显示记录
		for (int i = 1; i <= userList.size(); i++) {
			out.write("<tr align='center'>      <td>" + i + "</td>      <td>" + userList.get(i - 1).getUserName()
					+ "</td>      <td>" + userList.get(i - 1).getUserDesc() + "</td>      <td><a href='"
					                              
					+ request.getContextPath() + "/servlet/updateuserpageforservlet?id=" + userList.get(i - 1).getId()
					+ "'>更新</a>&nbsp;&nbsp;&nbsp;&nbsp;<a href='" + request.getContextPath()
					+ "/servlet/deleteuserforservlet?id=" + userList.get(i - 1).getId()
					+ "'>删除</a>&nbsp;&nbsp;&nbsp;&nbsp;     ");
			
			
			
			String user_flag=userList.get(i - 1).getUserFlag().equals("1")?"禁用":"启用";
			String user_flag_code=userList.get(i - 1).getUserFlag().equals("1")?"0":"1";
			
			out.write("<a href='"+request.getContextPath()+"/servlet/updateuserflagforservlet?id="+userList.get(i - 1).getId()+"&user_flag="+user_flag_code+"'>"+user_flag+"</a>");
			
			out.write(" </td></tr>");
			
			
		}
		// 显示分页
		out.write("<tr align='right'><td colspan='4'>   共" + pageUtilForUser.getPageNum() + "页&nbsp;&nbsp;&nbsp;");
		out.write("<a href='" + request.getContextPath()
				+ "/servlet/finduserpageforservlet?currentPage=1'>第一页</a>&nbsp;&nbsp;&nbsp;");
		out.write("<a href='" + request.getContextPath() + "/servlet/finduserpageforservlet?currentPage="
				+ pageUtilForUser.getPrePage() + "'>上一页</a>&nbsp;&nbsp;&nbsp;");

		for (int i = 1; i <= pageUtilForUser.getPageNum(); i++) {
			out.write("<a href='" + request.getContextPath() + "/servlet/finduserpageforservlet?currentPage=" + i + "'>"
					+ i + "</a>&nbsp;&nbsp;&nbsp;");
		}

		out.write("<a href='" + request.getContextPath() + "/servlet/finduserpageforservlet?currentPage="
				+ pageUtilForUser.getNextpage() + "'>下一页</a>&nbsp;&nbsp;&nbsp;");
		out.write("<a href='" + request.getContextPath() + "/servlet/finduserpageforservlet?currentPage="
				+ pageUtilForUser.getPageNum() + "'>最后一页</a>&nbsp;&nbsp;&nbsp;");
		out.write("</td></tr>");

		out.write("</table> </body> </html>");
		logger.debug(" FindUserPageForServlet >doGet end.......");
	}

	/**
	 * 
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		logger.debug(" FindUserPageForServlet >doPost start.......");
		this.doGet(request, response);

		logger.debug(" FindUserPageForServlet >doPost end.......");
	}

}